package com.bigflexlib.x.Math.range
{
	public class RangeSet
	{
		
		
		public static const INCLUDE_NONE	:int= 0;
		public static const INCLUDE_MIN	:int= 1;
		public static const INCLUDE_MAX	:int= 2;
		
		
		
		private var _ranges:Array;
		
		public function get ranges( ):Array
		{
			return _ranges;
		}
		
		
		public function RangeSet( ranges:Array = null )
		{
			if ( ranges == null )
				ranges = new Array();
				
			_ranges = ranges;
		}
		
		
		
		/**
		 * 
		 * Returns the first range containing the given value or NULL
		 * 
		 * @param value
		 * @return 
		 * 
		 */		
		public function getFirstRangeContainingValue( value:Number ):Range
		{
		
			for each ( var range:Range in ranges )
				if ( range.containsValue( value ) )
					return range;

			return null;
		
		}
		
		
		public static function fromSeries( series:Array, includeFlag:int = INCLUDE_MIN ):RangeSet
		{
			
			var iMin:Boolean = includeFlag == INCLUDE_MIN;
			var iMax:Boolean = includeFlag == INCLUDE_MAX;
			
			if ( series.length < 2 )
				return new RangeSet( );
			
			var limits:Array = [ ];
			var ranges:Array = [ ];
			
			for ( var i:int=0; i<series.length; i++ )
			{
				limits.push( series[ i ] );
				if ( limits.length == 2 )
				{
					ranges.push( new Range( limits[0], limits[1], iMin, iMax ) );					
					limits.shift( );
				}
			}
			
			return new RangeSet( ranges );
		
		}

	}
}